import axios from 'axios'
import router from '@/router'
import { ElMessage } from 'element-plus'

//创建axios实例对象
const request = axios.create({
  baseURL: '/api',
  timeout: 600000
})

// request.interceptors.request.use(function (config) {
//   let userInfo = JSON.parse(localStorage.getItem('loginUser'))
//   if (userInfo) {
//     config.headers.token = userInfo.token
//   }
//   return config
// })

//axios的响应 response 拦截器
request.interceptors.response.use(
  (response) => { //成功回调
    return response.data
  },
  (error) => { //失败回调
    const status = error.response?.status
    if (status === 401) {
      ElMessage.error('身份信息失效！')
      router.push('/login')
    }
    return Promise.reject(error)
  }
)

export default request